﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using Dapper;
using System.Data;
using UpdateDateByAc.Entity;
using System.Configuration;

namespace UpdateDateByAc
{
    public class Connection
    {
        public IDbConnection GetConnection { get; set; }
        /// <summary>
        /// 默认连接数据库
     
        public Connection(string connectStr = "")
        {
            string constring = ConfigurationManager.ConnectionStrings["sqlstr"].ConnectionString.ToString();
            string connType = "mssql";
            IDbConnection _conn = null;
            switch (connType)
            {
                case "mssql":

                    if (string.IsNullOrWhiteSpace(connectStr))
                        _conn = new SqlConnection(constring);
                    else
                        _conn = new SqlConnection(connectStr);
                    break;
                case "mysql": break;
                case "oracle":
                    break;
                case "sqllite":
                    break;
                default:
                    break;
            }
            _conn.Open();
            GetConnection = _conn;
        }

    }
    public class Comm
    {      
        /*更改时间模块*/
        public static List<Ybxx> Getrqtime(string ybid )
        {
            //string constr = "data source=192.168.20.251;initial catalog=lisdb;user id=lisuser;pwd=lisuser";
            string constr = ConfigurationManager.ConnectionStrings["sqlstr"].ToString();
            
            IDbConnection conn = new SqlConnection(constr);

            return conn.Query<Ybxx>("select cjtime,bgrq,cyrq from lis_Ybxx where ybid = @ybid",new{ ybid}).ToList();
       
        }
        /*获取报告日期*/
        public static DateTime GetbgRq(string ybid)
        {
            string constr = ConfigurationManager.ConnectionStrings["sqlstr"].ConnectionString.ToString(); ;
            IDbConnection conn = new SqlConnection(constr);


            Ybxx datainfo = new Ybxx();

            string sql_insert = @"select bgrq from lis_ybxx where ybid=@ybid";
            var result = conn.QueryFirstOrDefault<Ybxx>(sql_insert, new { ybid = ybid });
            
            DateTime bgrqtime = result.Bgrq;

            return bgrqtime;
            
            
        }
        /// <summary>
        /// 更新日期
        /// </summary>
        /// <param name="ybid"></param>
        /// <returns></returns>
        public static int UpdateBg(string ybid)
        {
            string constr = ConfigurationManager.ConnectionStrings["sqlstr"].ToString();
            IDbConnection conn = new SqlConnection(constr);




            ReportTasks Report = new ReportTasks();
            Report.Barcode = ybid;

            string sql_update = @"UPDATE dbo.Lis_ReportTasks SET ProcessFlag = 0 WHERE Barcode = @Barcode";

            var result = conn.Execute(sql_update, Report);


            return result;
        }
        /// <summary>
        /// 更新日期
        /// </summary>
        /// <param name="cjRq"></param>
        /// <param name="cyRq"></param>
        /// <param name="bgRq"></param>
        /// <param name="ybid"></param>
        /// <returns></returns>
        public static int UpdateTime(DateTime cjRq, DateTime cyRq , DateTime bgRq, String ybid)
        {
            string constr = ConfigurationManager.ConnectionStrings["sqlstr"].ToString();
            IDbConnection conn = new SqlConnection(constr);

            Ybxx SDateTime = new Ybxx();
            SDateTime.Cyrq  = cyRq;
            SDateTime.Bgrq   = bgRq;
            SDateTime.Cjtime = cjRq;
            SDateTime.Ybid = ybid;




            string sql_update = @"UPDATE dbo.Lis_ybxx SET cjtime =@cjtime,bgrq = @bgrq ,cyrq=@cyrq WHERE ybid = @ybid";

            var result = conn.Execute(sql_update, SDateTime);


            return result;
        }

        /*************************************************************/
        public static List<yymc> GetHospital(string dh)
        {
            using(var conn = new Connection().GetConnection)
            {
               
                string sql = "select dh,mc,password from xt_yymc_print where dh = @dh";
                return conn.Query<yymc>(sql, new { dh = dh}).ToList();   
            }
        }

        /// <summary>
        /// 
        /// 增加医院
        /// </summary>
        /// <param name="dh"></param>
        /// <param name="mc"></param>
        /// <returns></returns>
        public static int InsertHos(string dh ,string mc)
        {
            using (var conn = new Connection().GetConnection)
            {
                yymc Datainfo = new yymc();
                Datainfo.Dh = dh;
                Datainfo.Mc = mc;
                string sql = "INSERT INTO xt_yymc_print ( dh, mc, title, ywy, ywzg, yydj, psfs, fsfs, printfs, loginname, password, PassWord2, issendromote, beizhu, dycs, sfjb, createdate ) VALUES ( @dh, @mc, 1, '', '', '4', '配送', '0', '1', @dh, @dh, '1', '0', '2', '1', '3', '2019-06-16 06:16:06.006' )";
                return conn.Execute(sql, Datainfo);

            }
        }

        public static int InsertUser(string logid ,string username,string copyuser )
        {

            using (var conn = new Connection().GetConnection)
            {
    
                string sql = @"select * from xt_user where username = @username ";

                var Dateinfo = conn.Query<User>(sql, new { username = copyuser }).ToList();

                if (Dateinfo.Count == 1)
                {


                    foreach (var aa in Dateinfo)
                    {


                        string addsql = "INSERT INTO xt_user(logid, username, password, power, status, department, tsxmsh, fshuncheck, zxid, gid, yqqx,ryqqx, djz, zyzqx, qdlis_login, qdlis_operate, Roletype, bbqr, bbjj, jyjs)VALUES(@logid,@username, @password, @power, @status, @department, @tsxmsh, @fshuncheck, @zxid, @gid, @yqqx,@ryqqx, @djz, @zyzqx, @qdlis_login, @qdlis_operate, @Roletype, @bbqr, @bbjj, @jyjs)";
                        User userinfo = new User();
                        userinfo.Logid = logid;//替换
                        userinfo.Username = username;//替换
                        userinfo.Password = logid;//替换
                        userinfo.Power = aa.Power;
                        userinfo.Status = aa.Status;
                        userinfo.Department = aa.Department;
                        userinfo.Fshuncheck = aa.Fshuncheck;
                        userinfo.Zxid = aa.Zxid;
                        userinfo.Gid = aa.Gid;
                        userinfo.Yqqx = aa.Yqqx;
                        userinfo.Ryqqx = aa.Ryqqx;
                        userinfo.Djz = aa.Djz;
                        userinfo.Qdlis_login = aa.Qdlis_login;
                        userinfo.Qdlis_operate = aa.Qdlis_operate;
                        userinfo.Bbqr = aa.Bbqr;
                        userinfo.Bbjj = aa.Bbjj;
                        userinfo.Jyjs = aa.Jyjs;
                        var res = conn.Execute(addsql, userinfo);
                        return 1;
                    }

                    //查询完了开始复制添加
                }
                else {
                    return 2;
                }
                return 3;



            }
        }

        //TAB2
        //查询密码及是否锁定

        public static List<User> GetPass(string logid,string username)
        {
            using (var conn = new Connection().GetConnection)
            {

                string sql = "select password,accountLock from xt_user where logid =@logid or username =@username";
                return conn.Query<User>(sql, new { logid = logid,username = username }).ToList();
            }
        }
        /// <summary>
        /// 判断是不是汉字--根据姓名查密码
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static bool CheckStringChineseUn(string text)
        {
            char[] c = text.ToCharArray();
            bool res = false;

            for (int i = 0; i < c.Length; i++)
            {
                if ((int)text[i] > 127)
                    res = true;
            }
            return res;
        }


        ///修改密码和锁定状态
        ///
        public static List<User> UpPass(string logid,string username)
        {
            using (var conn = new Connection().GetConnection)
            {
                string sql = "";
            }
                return null;
        }


        //删除重复录入数据
        public static int DellInputDate(string ybid)
        {
            string sql = @"DELETE FROM dbo.f_k_ybxx WHERE ybid =@ybid";
            string sql2 = @"delete FROM dbo.f_k_jymd WHERE ybid = @ybid";
            string sql3 = @"delete FROM dbo.t_lis_sampletransPro where ybid = @ybid";
            try
            {
                using (var conn = new Connection().GetConnection)
                {
                    conn.Execute(sql, new { ybid = ybid });
                    conn.Execute(sql2, new { ybid = ybid });
                    conn.Execute(sql3, new { ybid = ybid });
                    return 1;
                }

            }
            catch (Exception)
            {

                return 2;
            }

    
        }

    }


 }
